/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import fr.ove.palette.swing.*;

/**
* A Toolbar under the menu bar.
*/
public class ToolBar extends JToolBar implements ChangeListener {
    /**
    * The WorkBook to operate with.
    */
    private WorkBook workBook;
    
    /**
    * The save button.
    */
    private JButton buttonSave;
    
    /**
    * The clear button.
    */
    private JButton buttonClear;
    
    /**
    * The delete button.
    */
    private JButton buttonTrash;
    
    /**
    * The purge button.
    */
    private JButton buttonPurge;
    
    /**
    * The palette button.
    */
    private JButton buttonPalette;
    
    /**
    * The OpenMath button.
    */
    private JButton buttonOM;
    
    /**
    * The run button.
    */
    private JButton buttonRun;
    
    /**
    * The run button.
    */
    private JButton buttonRunAll;
    
    /**
    * The OpenMath viewer.
    */
    private OpenMathViewer omViewer;
    
    /**
    * The palette editor.
    */
    private JPaletteEditor palette;
    
    /**
    * The panel command for typing in the linear form of the formula
    */
    private PanelCommand panelCommand;
    
    public ToolBar(WorkBook wBook, OpenMathViewer viewer, JPaletteEditor jpalette, PanelCommand panelComm) {
        workBook = wBook;
        omViewer = viewer;
        palette = jpalette;
        panelCommand = panelComm;
        
        JButton button = new JButton(getIcon("fr/ove/openmath/mathematica/images/new.gif"));
		button.setDefaultCapable(false);
		button.setToolTipText("Create a new worksheet");
		button.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.addTab();
                }
            }
        );
		add(button);
		
		button = new JButton(getIcon("fr/ove/openmath/mathematica/images/open.gif"));
		button.setDefaultCapable(false);
		button.setToolTipText("Open an existing worksheet");
		button.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
		            workBook.open();
                }
            }
        );
		add(button);
		
		buttonSave = new JButton(getIcon("fr/ove/openmath/mathematica/images/save.gif"));
		buttonSave.setDefaultCapable(false);
		buttonSave.setToolTipText("Save the active worksheet");
		buttonSave.setEnabled(false);
		buttonSave.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.save();
                }
            }
        );
		add(buttonSave);
		
		add(new JToolBar.Separator());
		
		buttonClear = new JButton(getIcon("fr/ove/openmath/mathematica/images/clear.gif"));
		buttonClear.setDefaultCapable(false);
		buttonClear.setToolTipText("Clear the current request");
		buttonClear.setEnabled(false);
		buttonClear.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    panelCommand.clear();
                }
            }
        );
		add(buttonClear);
		
		buttonTrash = new JButton(getIcon("fr/ove/openmath/mathematica/images/trash.gif"));
		buttonTrash.setDefaultCapable(false);
		buttonTrash.setToolTipText("Remove the current request");
		buttonTrash.setEnabled(false);
		buttonTrash.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JInternalFrame jif = workBook.getActiveInternalFrame();
                    OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
                    omSheet./*initialize*/removeCurrent();
                }
            }
        );
		add(buttonTrash);
		
		buttonPurge = new JButton(getIcon("fr/ove/openmath/mathematica/images/basket.gif"));
		buttonPurge.setDefaultCapable(false);
		buttonPurge.setToolTipText("Clear the work sheet");
		buttonPurge.setEnabled(false);
		buttonPurge.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JInternalFrame jif = workBook.getActiveInternalFrame();
                    OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
                    omSheet.initialize();
                }
            }
        );
		add(buttonPurge);
		
		add(new JToolBar.Separator());
		
		buttonPalette = new JButton(getIcon("fr/ove/openmath/mathematica/images/palette.gif"));
		buttonPalette.setDefaultCapable(false);
		buttonPalette.setToolTipText("Open the palette editor");
		buttonPalette.setEnabled(false);
		buttonPalette.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.showPalette("Palette Editor");
                }
            }
        );
		add(buttonPalette);
		
		buttonOM = new JButton(getIcon("fr/ove/openmath/mathematica/images/show2.gif"));
		buttonOM.setDefaultCapable(false);
		buttonOM.setToolTipText("View the OpenMath objects");
		buttonOM.setEnabled(false);
		buttonOM.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    omViewer.setVisible(!omViewer.isVisible());
                    if (omViewer.isVisible())
                        updateOmViewer();
                }
            }
        );
		add(buttonOM);
		
		add(new JToolBar.Separator());
		
		buttonRun = new JButton(getIcon("fr/ove/openmath/mathematica/images/run.gif"));
		buttonRun.setDefaultCapable(false);
		buttonRun.setToolTipText("Run the current calculation");
		buttonRun.setEnabled(false);
		buttonRun.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JInternalFrame jif = workBook.getActiveInternalFrame();
                    OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
                    omSheet.runCurrentRequest();
                }
            }
        );
		add(buttonRun);
		
		buttonRunAll = new JButton(getIcon("fr/ove/openmath/mathematica/images/runall2.gif"));
		buttonRunAll.setDefaultCapable(false);
		buttonRunAll.setToolTipText("Run the work sheet");
		buttonRunAll.setEnabled(false);
		buttonRunAll.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JInternalFrame jif = workBook.getActiveInternalFrame();
                    OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
                    omSheet.runWorkSheet();
                }
            }
        );
		add(buttonRunAll);
    }
    
    /**
    * Returns the icon with the specified image filename.
    * @param filename the image filename
    */
    private ImageIcon getIcon(String filename) {
        ImageIcon icon = null;
        
        java.net.URL iconURL = ClassLoader.getSystemResource(filename);
        if (iconURL != null)
            icon = new ImageIcon(iconURL);
            
        return icon;
    }
    
    /**
    * Updates the OpenMath viewer.
    */
    private void updateOmViewer() {
        JInternalFrame jif = workBook.getActiveInternalFrame();
        OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
        PanelJomes panelJomes = omSheet.getCurrentElement();
        omViewer.setOpenMathRequest(panelJomes.getOpenMathRequest());
        omViewer.setOpenMathResult(panelJomes.getOpenMathResult());
    }
    
    
    //  ### ChangeListenerInterface implementation
    
    /**
    * Invoked when the target of the listener has changed its state.
    * @param e a ChangeEvent object 
    */
    public void stateChanged(ChangeEvent e) {
        if (workBook.getTabCount() == 0) {
            buttonSave.setEnabled(false);
    		buttonClear.setEnabled(false);
    		buttonTrash.setEnabled(false);
    		buttonPurge.setEnabled(false);
    		buttonPalette.setEnabled(false);
            buttonOM.setEnabled(false);
            omViewer.setVisible(false);
            buttonRun.setEnabled(false);
            buttonRunAll.setEnabled(false);
        }
        else {
            if (!buttonSave.isEnabled()) {
                // If one is enabled, others are also. So...
                buttonSave.setEnabled(true);
    		    buttonClear.setEnabled(true);
    		    buttonTrash.setEnabled(true);
    		    buttonPurge.setEnabled(true);
    		    buttonPalette.setEnabled(true);
                buttonOM.setEnabled(true);
                buttonRun.setEnabled(true);
                buttonRunAll.setEnabled(true);
            }
            
            if (omViewer.isVisible())
                updateOmViewer();
                
        }
    }
}